package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.entities.LpCourseTbl;
import cn.com.lsy.product.lepao.model.entities.LpCourseTypeTbl;
import cn.com.lsy.product.lepao.model.entities.LpCourseVideoTbl;
import cn.com.lsy.product.lepao.request.*;
import cn.com.lsy.product.lepao.response.*;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.response.base.PageInfo;
import cn.com.lsy.product.lepao.service.ICourseService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "课程中心接口", tags = "课程中心接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class CourseController extends BaseController {

    private Logger log = Logger.getLogger(CourseController.class);
    Mapper mapper = new DozerBeanMapper();

    @Autowired
    ICourseService iCourseService;

    @RequestMapping(value = "/courseTypeList", method = RequestMethod.POST)
    @ApiOperation(notes = "课程分类列表", httpMethod = "POST", value = "课程分类列表")
    public BaseRet<List<CourseTypeInfoRet>> courseTypeList() {

        BaseRet<List<CourseTypeInfoRet>> ret = new BaseRet<List<CourseTypeInfoRet>>();

        try {

            List<CourseTypeInfoRet> listRet = new ArrayList<CourseTypeInfoRet>();

            List<LpCourseTypeTbl> list = iCourseService.selectCourseType();

            if (list != null && list.size() > 0) {

                for (LpCourseTypeTbl value : list) {

                    CourseTypeInfoRet courseTypeInfoRet = new CourseTypeInfoRet();

                    String createTime = CommonFunctionUtil.changeTimeString(value.getCreateTime(), Constant.formatType.second);
                    String updateTime = CommonFunctionUtil.changeTimeString(value.getUpdateTime(), Constant.formatType.second);
                    value.setCreateTime(null);
                    value.setUpdateTime(null);

                    mapper.map(value, courseTypeInfoRet);
                    courseTypeInfoRet.setCreateTime(createTime);
                    courseTypeInfoRet.setUpdateTime(updateTime);
                    listRet.add(courseTypeInfoRet);
                }
            }

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(listRet);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addCourseType", method = RequestMethod.POST)
    @ApiOperation(notes = "添加课程分类", httpMethod = "POST", value = "添加课程分类")
    public BaseRet<String> addCourseType(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddCourseTypeParam param,
                                         BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTypeTbl lpCourseTypeTbl = new LpCourseTypeTbl();
            lpCourseTypeTbl.setName(param.getName());
            lpCourseTypeTbl.setStatus(Constant.status.enable);
            lpCourseTypeTbl.setCreateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpCourseTypeTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iCourseService.insertCourseType(lpCourseTypeTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourseType", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程分类", httpMethod = "POST", value = "修改课程分类")
    public BaseRet<String> updateCourseType(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseTypeParam param,
                                            BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTypeTbl lpCourseTypeTbl = new LpCourseTypeTbl();
            lpCourseTypeTbl.setId(param.getId());
            lpCourseTypeTbl.setName(param.getName());

            if (iCourseService.updateCourseType(lpCourseTypeTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourseTypeStatus", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程分类状态", httpMethod = "POST", value = "修改课程分类状态")
    public BaseRet<String> updateCourseTypeStatus(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseTypeStatusParam param,
                                                  BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTypeTbl lpCourseTypeTbl = new LpCourseTypeTbl();
            lpCourseTypeTbl.setId(param.getId());
            lpCourseTypeTbl.setStatus(String.valueOf(param.getStatus()));

            if (iCourseService.updateCourseType(lpCourseTypeTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/courseList", method = RequestMethod.POST)
    @ApiOperation(notes = "获取课程列表", httpMethod = "POST", value = "获取课程列表")
    public BaseRet<PageInfo<List<CourseListRet>>> courseList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody CourseListParam param,
                                                             BindingResult result) {

        BaseRet<PageInfo<List<CourseListRet>>> ret = new BaseRet<PageInfo<List<CourseListRet>>>();

        PageInfo<List<CourseListRet>> pageInfo = new PageInfo<List<CourseListRet>>();

        try {

            checkParam(result);

            int count = iCourseService.selectCourseByCount(param.getTypeId(), param.getSearchCondition());
            pageInfo.setCount(count);
            int start = param.getStart();

            List<LpCourseTbl> list = iCourseService.selectCourseByPage(start, param.getPageSize(), param.getTypeId(), param.getSearchCondition());

            List<CourseListRet> listRet = new ArrayList<CourseListRet>();

            if (list != null && list.size() > 0) {

                for (LpCourseTbl value : list) {

                    CourseListRet courseListRet = new CourseListRet();

                    String createTime = CommonFunctionUtil.changeTimeString(value.getCreateTime(), Constant.formatType.second);
                    value.setCreateTime(null);

                    mapper.map(value, courseListRet);
                    courseListRet.setCreateTime(createTime);
                    listRet.add(courseListRet);
                }
            }

            pageInfo.setRecords(listRet);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(pageInfo);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/courseInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "获取课程详情", httpMethod = "POST", value = "获取课程详情")
    public BaseRet<CourseInfoRet> courseInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
                                             BindingResult result) {

        BaseRet<CourseInfoRet> ret = new BaseRet<CourseInfoRet>();

        try {

            checkParam(result);

            CourseInfoRet courseInfoRet = new CourseInfoRet();

            LpCourseTbl info = iCourseService.selectCourseInfo(param.getId());
            int isHot = Integer.valueOf(info.getIsHot());
            info.setIsHot(null);
            mapper.map(info, courseInfoRet);
            courseInfoRet.setIsHot(isHot);
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(courseInfoRet);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addCourse", method = RequestMethod.POST)
    @ApiOperation(notes = "添加课程信息", httpMethod = "POST", value = "添加课程信息")
    public BaseRet<String> addCourse(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddCourseParam param,
                                     BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTbl lpCourseTbl = new LpCourseTbl();
            mapper.map(param, lpCourseTbl);
            lpCourseTbl.setCreateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpCourseTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpCourseTbl.setStatus(Constant.status.enable);
            lpCourseTbl.setCompleteNum(0);

            if (iCourseService.insertCourse(lpCourseTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourse", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程信息", httpMethod = "POST", value = "修改课程信息")
    public BaseRet<String> updateCourse(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseParam param,
                                        BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTbl lpCourseTbl = new LpCourseTbl();
            mapper.map(param, lpCourseTbl);
            lpCourseTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iCourseService.updateCourse(lpCourseTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourseStatus", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程状态", httpMethod = "POST", value = "修改课程状态")
    public BaseRet<String> updateCourseStatus(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseTypeStatusParam param,
                                              BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseTbl lpCourseTbl = new LpCourseTbl();
            lpCourseTbl.setStatus(String.valueOf(param.getStatus()));
            lpCourseTbl.setId(param.getId());

            lpCourseTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iCourseService.updateCourse(lpCourseTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/courseVideoList", method = RequestMethod.POST)
    @ApiOperation(notes = "课程动作列表", httpMethod = "POST", value = "课程动作列表")
    public BaseRet<PageInfo<List<CourseVideoListRet>>> courseVideoList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody CourseVideoListParam param,
                                                                       BindingResult result) {

        BaseRet<PageInfo<List<CourseVideoListRet>>> ret = new BaseRet<PageInfo<List<CourseVideoListRet>>>();

        PageInfo<List<CourseVideoListRet>> pageInfo = new PageInfo<List<CourseVideoListRet>>();

        try {

            checkParam(result);

            int count = iCourseService.selectCourseVideoByCount(param.getCourseId(), param.getSearchCondition());
            pageInfo.setCount(count);
            int start = param.getStart();

            List<LpCourseVideoTbl> list = iCourseService.selectCourseVideoByPage(start, param.getPageSize(), param.getCourseId(), param.getSearchCondition());

            List<CourseVideoListRet> listRet = new ArrayList<CourseVideoListRet>();

            if (list != null && list.size() > 0) {

                for (LpCourseVideoTbl value : list) {

                    CourseVideoListRet courseVideoListRet = new CourseVideoListRet();

                    String createTime = CommonFunctionUtil.changeTimeString(value.getCreateTime(), Constant.formatType.second);
                    value.setCreateTime(null);

                    mapper.map(value, courseVideoListRet);
                    courseVideoListRet.setCreateTime(createTime);
                    listRet.add(courseVideoListRet);
                }
            }

            pageInfo.setRecords(listRet);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(pageInfo);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/courseVideoInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "课程动作详情信息", httpMethod = "POST", value = "课程动作详情信息")
    public BaseRet<CourseVideoInfoRet> courseVideoInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
                                                       BindingResult result) {

        BaseRet<CourseVideoInfoRet> ret = new BaseRet<CourseVideoInfoRet>();

        try {

            checkParam(result);

            CourseVideoInfoRet courseVideoInfoRet = new CourseVideoInfoRet();

            LpCourseVideoTbl info = iCourseService.selectCourseVideoInfo(param.getId());
            mapper.map(info, courseVideoInfoRet);

            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");
            ret.setData(courseVideoInfoRet);


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourseVideo", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程动作信息", httpMethod = "POST", value = "修改课程动作信息")
    public BaseRet<String> updateCourseVideo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseVideoParam param,
                                             BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseVideoTbl lpCourseVideoTbl = new LpCourseVideoTbl();
            mapper.map(param, lpCourseVideoTbl);
            lpCourseVideoTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iCourseService.updateCourseVideo(lpCourseVideoTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateCourseVideoStatus", method = RequestMethod.POST)
    @ApiOperation(notes = "修改课程动作状态", httpMethod = "POST", value = "修改课程动作状态")
    public BaseRet<String> updateCourseVideoStatus(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateCourseTypeStatusParam param,
                                                   BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseVideoTbl lpCourseVideoTbl = new LpCourseVideoTbl();
            lpCourseVideoTbl.setId(param.getId());
            lpCourseVideoTbl.setStatus(String.valueOf(param.getStatus()));
            lpCourseVideoTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));

            if (iCourseService.updateCourseVideo(lpCourseVideoTbl)) {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("修改成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/addCourseVideo", method = RequestMethod.POST)
    @ApiOperation(notes = "添加课程动作信息", httpMethod = "POST", value = "添加课程动作信息")
    public BaseRet<String> addCourseVideo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody AddCourseVideoParam param,
                                          BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpCourseVideoTbl lpCourseVideoTbl = new LpCourseVideoTbl();
            mapper.map(param, lpCourseVideoTbl);
            lpCourseVideoTbl.setUpdateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpCourseVideoTbl.setCreateTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            lpCourseVideoTbl.setStatus(Constant.status.enable);

            if (iCourseService.insertCourseVideo(lpCourseVideoTbl)) {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);

            } else {

                ret.setMsg("添加成功");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }

}
